4. Scripting Styles
Changing content with JavaScript is useful, but sometimes
it’s easier to use styles from JavaScript to make things appear and
disappear and change their appearance.
4.1. Changing styles
With DOM support, we can read and dynamically change every inner
CSS style using style and its
subproperties, like backgroundColor, textAlign, and margin. We can also change CSS styles using
the className property of every
element. Table 20 explores which
browsers support changing the class, removing the class, and applying
multiple classes using a space (e.g., class1
class2).
Table 20. Changing CSS dynamically compatibility table
Browser/platform | Support for changing
styles and classes dynamically |
---|
Safari | Yes |
Android
browser | Yes |
Symbian/S60 | Yes |
Nokia Series
40 | No before
6th edition |
webOS | Yes |
BlackBerry | No before
4.6 |
NetFront | Yes |
Internet
Explorer | Yes |
Motorola Internet
Browser | Partial |
Opera
Mobile | Yes |
Opera
Mini | Yes |
4.2. Showing and hiding styles
Table 21
shows which browsers support showing and hiding block content using
element.style.display='none' or
element.style.display='block'. We
can also use style.visibility, but
in this case the block will still occupy the box without showing its
contents.
Table 21. Showing and hiding elements compatibility table
Browser/platform | Support for
showing/hiding content |
---|
Safari | Yes |
Android
browser | Yes |
Symbian/S60 | Yes |
Nokia Series
40 | No before
6th edition |
webOS | Yes |
BlackBerry | No before
4.6 |
NetFront | Yes |
Internet
Explorer | Yes |
Motorola Internet
Browser | No |
Opera
Mobile | Yes |
Opera
Mini | Yes |